package com.ken.quartz.scheduler;

import com.ken.quartz.job.HelloJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

/**
 *
 * Created by renchongyu on 2018/11/24.
 */
public class HelloScheduler {

    public static void main(String[] args) throws Exception{
        Date startDate = new Date();
        Date endDate = new Date();
        //创建一个JobDetail实例，将该实例与HelloJob Class绑定
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
                .withIdentity("myJob")
                .usingJobData("message","hello myJob1")//自定义参数1
                .usingJobData("FloatJobValue",3.14f)//自定义参数2
                .build();
        //jobDetail可以获得任务的属性值
        System.out.println("name="+jobDetail.getKey().getName());
        System.out.println("group="+jobDetail.getKey().getGroup());
        System.out.println("jobClass="+jobDetail.getKey().getClass());
        /**
         * 创建一个Trigger实例:若使用startNow()表定义该job立即执行，并且每隔两秒钟重复执行一次，直到永远。
         * 若使用startAt和endAt表开始和结束时间
        */
        startDate.setTime(startDate.getTime()+3000l);//3秒钟后执行
        endDate.setTime(endDate.getTime()+9000l);//9秒钟后结束
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger","group1")
                .usingJobData("message","hello myTrigger1")//自定义传参1
                .usingJobData("DoubleTriggerValue",2.0d)//自定义传参2
                //.startNow()//表立即执行,且不停止。
                .startAt(startDate)//开始时间
                .endAt(endDate)//结束时间
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(2)
                .repeatForever())
                .build();
        //创建Scheduler实例
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();

        scheduler.scheduleJob(jobDetail,trigger);
    }
}
